/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.devtools.simple.compiler.symbols;

import com.google.devtools.simple.classfiles.Method;
import com.google.devtools.simple.compiler.Compiler;

/**
 * Superclass for all symbols.
 * 
 * @author Herbert Czymontek
 */
public abstract class Symbol {

  //Source code start position of symbol
  private final long position;

  // Symbol name
  private final String name;

  /**
   * Creates a new symbol.
   *
   * @param position  source code start position of symbol 
   * @param name  symbol name
   */
  public Symbol(long position, String name) {
    this.position = position;
    this.name = name;
  }

  /**
   * Indicates whether the symbol was generated by the compiler to recover from
   * an error.
   *
   * @return  {@code true} for all 'regular' symbols, {@code false} otherwise
   */
  public boolean isErrorSymbol() {
    return false;
  }

  /**
   * Returns the source code position of the expression.
   *
   * @return  source code start position of expression
   */
  public long getPosition() {
    return position;
  }

  /**
   * Returns the actual symbol. This method is only overridden by the
   * {@link AliasSymbol}. 
   * 
   * @return  symbol
   */
  public Symbol getActualSymbol() {
    return this;
  }

  /**
   * Resolves any types involved with the symbol.
   * 
   * @param compiler  current compiler instance
   * @param currentFunction  current function being resolved
   *                         (can be {code null})
   */
  public void resolve(Compiler compiler, FunctionSymbol currentFunction) {
  }

  /**
   * Generates code for reading from the symbols storage location.
   * 
   * @param m  method to generate the code for
   */
  public abstract void generateRead(Method m);

  /**
   * Generates code for writing to the symbols storage location.
   * 
   * @param m  method to generate the code for
   */
  public abstract void generateWrite(Method m);

  /**
   * Returns the name of the symbol.
   * 
   * @return  symbol name
   */
  public final String getName() {
    return name;
  }

  @Override
  public String toString() {
    return name;
  }
}
